import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

import taxicall.entities.NetworkMessage;
import taxicall.entities.NetworkMessage.MessageType;
import taxicall.mocks.TaxicallMocks;
import taxicall.server.Server;

public class Main {

	public static void main(String[] args) throws Exception {
		new Thread(new Runnable(){
			@Override
			public void run() {
				try {
					Server.main(null);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}).start();
		Thread.sleep(1000);
		
		sendTestPacket();
	}
	
	public static void sendTestPacket() throws Exception {
		byte[] buffer = new byte[4096];
		DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

		DatagramSocket socket = new DatagramSocket();
		
		MessageType type = MessageType.SetStartLocations;
		Serializable dataObject = (Serializable) new TaxicallMocks().getStartLocations();
		NetworkMessage  m = new NetworkMessage(type, dataObject);
		
		ByteArrayOutputStream bStream = new ByteArrayOutputStream();
		ObjectOutputStream oo = new ObjectOutputStream(bStream); 
		oo.writeObject(m);
		oo.close();
		
		packet.setData(bStream.toByteArray());
		packet.setAddress(InetAddress.getLocalHost());
		packet.setPort(4000);
		socket.send(packet);
		System.out.println("Packet sent");
		
		socket.close();
	}

}
